package com.gzwh.weblib.utils;


import android.app.Activity;
import android.content.Context;
import android.os.Build;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.gzwh.weblib.bean.CountCateToFoodModel;
import com.gzwh.weblib.bean.DMenuModel;
import com.gzwh.weblib.bean.DTreeCategoryModel;
import com.gzwh.weblib.bean.DinnerFood;
import com.gzwh.weblib.bean.DinnerNzVerify;
import com.gzwh.weblib.bean.DinnerOrderModel;
import com.gzwh.weblib.bean.DinnerShopStaff;
import com.gzwh.weblib.bean.HandSetDevicesModel;
import com.gzwh.weblib.bean.MenuFoodModel;
import com.gzwh.weblib.bean.NzDinnerQrcode;
import com.gzwh.weblib.bean.NzDinnerRegion;
import com.gzwh.weblib.bean.NzProductAttrModel;
import com.gzwh.weblib.bean.NzProductCategory;
import com.gzwh.weblib.bean.NzStore;
import com.gzwh.weblib.bean.PayCheckModel;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.bean.SQuery;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;
import com.wenhuitec.syslib.config.SysConfig;
import com.wenhuitec.syslib.utils.SPUtils;
import com.wenhuitec.syslib.utils.SysLoginUtils;

import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class RequestUtils {

    /**
     * 获取系统识别号
     *
     * @param context
     * @param username
     * @param password
     * @param ver
     * @param observer
     */
    public static void getLogin(Context context, String username, String password, String ver, MyObserver<String> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("userName", username);
            root.put("password", password);
            root.put("ver", "1");
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().getLogin(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void storePrice(Context activity, MyObserver<Map<String, Object>> observer) {
        RetrofitUtils.getApiUrl().storePrice(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getHome(Context activity, MyObserver<com.alibaba.fastjson.JSONObject> observer) {
        RetrofitUtils.getApiUrl().getHome(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }
    public static void getStore(Context activity, MyObserver<NzStore> observer) {
        RetrofitUtils.getApiUrl().getStore(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getCategoryList(Context activity, MyObserver<List<NzProductCategory>> observer) {
        RetrofitUtils.getApiUrl().getCategoryList(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getQrList(Context activity,String areId, MyObserver<List<NzDinnerQrcode>> observer) {
        RetrofitUtils.getApiUrl().getQrList(SysLoginUtils.getToken(),areId).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }
    public static void getAreList(Context activity, MyObserver<List<NzDinnerRegion>> observer) {
        RetrofitUtils.getApiUrl().getAreList(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void addAre(Context activity, String name, MyObserver<String> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("name", name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().addAre(requestBody, SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void deleteAre(Context activity, String areId, MyObserver<String> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("areId", areId);

        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().deleteAre(requestBody, SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void updateAre(Context activity, String areId, String name, MyObserver<String> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("areId", areId);
            root.put("name", name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().updateAre(requestBody, SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getAttrList(Context activity, MyObserver<List<NzProductAttrModel>> observer) {
        RetrofitUtils.getApiUrl().getAttrList(SysLoginUtils.getToken()).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getProductList(Context activity,String categoryId, MyObserver<JSONArray> observer) {
        RetrofitUtils.getApiUrl().getProductList(SysLoginUtils.getToken(),categoryId).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }


    //获取系统基本参数
    public static void getSysInfo(Activity activity, String verify, String channelId, MyObserver<Map<String, String>> observer) {
        RetrofitUtils.getApiUrl().getSysInfo(verify, channelId).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    //获取登陆人员
    public static void getSysUser(Context activity, String verify, String channelId, MyObserver<List<DinnerShopStaff>> observer) {
        RetrofitUtils.getApiUrl().getSysUser(verify, channelId).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }

    public static void getMenuFoods(Activity activity, String verify, String channelId, MyObserver<DMenuModel> observer) {
        RetrofitUtils.getApiUrl().getMenuFoods(verify, channelId).compose(RxHelper.observableIO2Main(activity)).subscribe(observer);
    }


    public static void micropay(Context context, String authCode, String channelId, String orders, String price, MyObserver<DMenuModel> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("authCode", authCode);
            root.put("channelId", channelId);
            root.put("orders", orders);
            root.put("price", price);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().micropay(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void query(Context context, String channelId, String orders, MyObserver<SQuery> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("channelId", channelId);
            root.put("orders", orders);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().query(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void addChannel(Context context, String channelId, String code, MyObserver<JSONObject> observer) {
        JSONObject root = new JSONObject();

        try {
            root.put("channelId", channelId);
            root.put("code", code);

        } catch (Exception e) {
            e.printStackTrace();

        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().addChannel(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void query2(Context context, String channelId, String orders, MyObserver<PayCheckModel> observer) {
        JSONObject root = new JSONObject();
        try {
            root.put("channelId", channelId);
            root.put("orders", orders);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        RetrofitUtils.getApiUrl().query2(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    //获取订单信息
    public static void getOrders(Context context, String sysChannelId, MyObserver<List<DinnerOrderModel>> observer) {
        RetrofitUtils.getApiUrl().getOrders(sysChannelId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getMyOrderCS(Context context, String sysChannelId, MyObserver<Map<String, String>> observer) {
        RetrofitUtils.getApiUrl().getMyOrderCS(sysChannelId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void login(Context context, String userName, String password, MyObserver<String> observer) {
        RetrofitUtils.getApiUrl().login(userName, password).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void regDevices(Context context, MyObserver<String> observer) {
        HandSetDevicesModel handSetDevicesModel = new HandSetDevicesModel();
        handSetDevicesModel.setImei(DeviceIdUtil.getIMEI(context));
        handSetDevicesModel.setModel(Build.MODEL);
        handSetDevicesModel.setDevice(Build.DEVICE);
        handSetDevicesModel.setProduct(Build.PRODUCT);
        handSetDevicesModel.setSerial(DeviceIdUtil.getSerial(context));
        handSetDevicesModel.setLongMac(String.valueOf(DeviceIdUtil.getLongMac()));
        handSetDevicesModel.setHardWare(Build.HARDWARE);
        handSetDevicesModel.setBoard(Build.BRAND);
        handSetDevicesModel.setAndroidVersion(Build.VERSION.RELEASE);
        handSetDevicesModel.setDevicesIds(DeviceIdUtil.DeviceIdUtil(context));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), com.alibaba.fastjson.JSONObject.toJSONString(handSetDevicesModel));
        RetrofitUtils.getApiUrl().regDevices(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void toLogin(Context context, String userName, String password, MyObserver<String> observer) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("password", password);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        RetrofitUtils.getApiUrl().toLogin(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void toLoginStaff(Context context, String userName, String password, String ecp, String deviceIds, MyObserver<String> observer) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("userName", userName);
            jsonObject.put("password", password);
            jsonObject.put("ecp", ecp);
            jsonObject.put("deviceIds", deviceIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString());
        RetrofitUtils.getApiUrl().toLoginStaff(requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void getFoodCategory(Context context, String menuId, MyObserver<List<DTreeCategoryModel>> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().getFoodCategory(userToken, menuId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void addFoodCategory(Context context, String name, String menuId, MyObserver<String> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().addFoodCategory(userToken, name, menuId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void getFood(Context context, String cateId, MyObserver<List<MenuFoodModel>> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().getFood(userToken, cateId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void countFoodNumByCategory(Context context, String menuId, MyObserver<List<CountCateToFoodModel>> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().countFoodNumByCategory(userToken, menuId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void saveCategorySort(Context context, String jsonarray, MyObserver<String> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonarray);
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().saveCategorySort(userToken, requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void upFoodShowStatus(Context context, String foodId, MyObserver<List<MenuFoodModel>> observer) {

        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().upFoodShowStatus(userToken, foodId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void deleteFoodCategory(Context context, String cateId, MyObserver<String> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().deleteFoodCategory(userToken, cateId).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    public static void updateFoods(Context context, String jsonarray, MyObserver<String> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonarray);
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().updateFoods(userToken, requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

    public static void updateStockBath(Context context, String jsonarray, MyObserver<String> observer) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), jsonarray);
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().updateStockBath(userToken, requestBody).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }


    //修改密码
    public static void updatePassword(Context context, String oldPassword, String newPassword, MyObserver<String> observer) {
        String userToken = (String) SPUtils.get(context, SysConfig.LOGINB_TOKEN, "");
        RetrofitUtils.getApiUrl().updatePassword(userToken, oldPassword, newPassword).compose(RxHelper.observableIO2Main(context)).subscribe(observer);
    }

}
